from fastapi import APIRouter, Query, Depends
# from sqlalchemy import select, text, and_
from sqlalchemy.orm import Session
from utils.response import success, fail
from utils.database import get_db
from app.config import get_settings

toolController = APIRouter()


@toolController.get("/gen/code")
async def get_code(table_name:str = Query(min_length=2, max_length=20),  db: Session = Depends(get_db)):
  settings = get_settings()
  
  ret = {
    'router'     : open(settings.root_path+'/data/tlp.router.code','r',encoding='utf8').read(),
    'entity'     : open(settings.root_path+'/data/tlp.entity.code','r',encoding='utf8').read(),
    'schema'     : open(settings.root_path+'/data/tlp.schema.code','r',encoding='utf8').read(),
    'model'      : open(settings.root_path+'/data/tlp.model.code','r',encoding='utf8').read(),
    'controller' : open(settings.root_path+'/data/tlp.controller.code','r',encoding='utf8').read(),
    'api.js'     : open(settings.root_path+'/data/tlp.api.js.code','r',encoding='utf8').read(),
    'vue'        : open(settings.root_path+'/data/tlp.vue.code','r',encoding='utf8').read(),
  }
  return success("OK", ret)
  # sql = select(text(
  #         'column_name,'
  #         '(CASE WHEN (is_nullable = "no" && column_key != "PRI") THEN "1" ELSE NULL END) AS is_required, '
  #         '(CASE WHEN column_key = "PRI" THEN "1" ELSE "0" END) AS is_pk, '
  #         'ordinal_position AS sort, column_comment, '
  #         '(CASE WHEN extra = "auto_increment" THEN "1" ELSE "0" END) AS is_increment, column_type')) \
  #         .where(
  #         and_(
  #             text('table_schema = (SELECT database())'),
  #             text(f'table_name = "{table_name}"'))) \
  #         .select_from(text('information_schema.columns')).order_by(text('ordinal_position'))
  # res = db.execute(sql)
  # fields = res.fetchall()
  
  # ret['router'] = '#找不到表格'
  # if fields==[]:
  #   return success("OK", ret)
  
  
  # db.query(SysPost).filter(SysPost.post_id == post.get('post_id')).update(post)
  # return success("操作成功")
